package com.sskj.hangqing.http;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.hangqing.bean.GuideListBean;
import com.sskj.hangqing.bean.HangqingBannerBean;
import com.sskj.hangqing.bean.HelpBean;
import com.sskj.hangqing.bean.NoticeBean;
import com.sskj.hangqing.bean.SelfCoinBean;
import com.sskj.hangqing.bean.Stock;
import com.sskj.hangqing.bean.SummaryBean;
import com.sskj.hangqing.bean.TradeBean;
import com.sskj.hangqing.bean.ZixunListBean;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.WSFiveBean;
import com.sskj.lib.util.CommonUtil;

import java.util.List;

import okhttp3.WebSocket;

/**
 * 作者 :吕志豪
 * 简书：https://www.jianshu.com/u/6e525b929aac
 * github：https://github.com/lvzhihao100
 * 描述：
 * 创建时间：2018-10-15 14:08
 */
public class HttpService {


    /**
     * 行情数据
     *
     * @param code 标识符
     * @return
     */
    public GetRequest<HttpData<List<CoinBean>>> getProduct(String code) {
        return OkGo.<HttpData<List<CoinBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_PRODUCT)
                .params("code", CommonUtil.dealReuqestCode(code));
    }

    /**
     *
     * @param code 标识符
     * @return
     */
    public GetRequest<HttpData<List<CoinBean>>> getProduct(String code, String type) {
        return OkGo.<HttpData<List<CoinBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_PRODUCT)
                .params("code", CommonUtil.dealReuqestCode(code))
                .params("type", type);
    }

    /**
     * 自选币列表
     *
     * @param code 标识符
     * @return
     */
    public GetRequest<HttpData<PageBean<SelfCoinBean>>> getSelfCoinList(String code, String page) {
        return OkGo.<HttpData<PageBean<SelfCoinBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_SELF_COIN)
                .params("code", CommonUtil.dealReuqestCode(code))
                .params("pageNumber", page)
                .params("pageSize", 20);
    }

    /**
     * 添加删除自选币
     *
     * @param id
     * @param status
     * @return
     */
    public PostRequest<HttpData<PageBean<SelfCoinBean>>> editSelfCoin(String id, String status) {
        return OkGo.<HttpData<PageBean<SelfCoinBean>>>post(HttpConfig.BASE_URL + HttpConfig.EDIT_SELF_COIN)
                .params("id", id)
                .params("status", status);
    }


    /**
     * 获取k线数据
     *
     * @param goodsType 分时数据标示
     * @param code      币种
     * @return
     */
    public PostRequest<HttpData<List<Stock>>> getKData(String goodsType, String code) {
        return OkGo.<HttpData<List<Stock>>>post(HttpConfig.BASE_URL + HttpConfig.GET_K_DATA)
                .params("pageSize", 500)
                .params("timeCycle", goodsType)
                .params("code", CommonUtil.dealReuqestCode(code))
                ;
    }


    /**
     * 获取轮播图及公告
     *
     * @return
     */
    public PostRequest<HttpData<List<HangqingBannerBean>>> getBanner(String type) {
        return OkGo.<HttpData<List<HangqingBannerBean>>>post(HttpConfig.BASE_URL + HttpConfig.GET_BANNER)
                .params("type", type)

                ;
    }


    /**
     * 实时成交
     *
     * @param code
     * @return
     */
    public WebSocketObserver pushCoinTrade(String code) {

        return new WebSocketObserver(HttpConfig.WS_BASE_URL) {
            @Override
            public void onOpen(WebSocket webSocket, okhttp3.Response response, String msg) {
                if (msg == null) {
                    webSocket.send("{\"type\":\"trader_info\",\"list\":[\"" + code + "\"]}");
                } else {
                    webSocket.send(msg);
                }
            }
        };
    }


    public GetRequest<HttpData<SummaryBean>> getSummary(String code) {
        return OkGo.<HttpData<SummaryBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_SUMMARY)
                .params("stockCode", CommonUtil.dealReuqestCode(code))
                ;
    }

    public GetRequest<HttpData<WSFiveBean>> getDeepData(String code) {
        return OkGo.<HttpData<WSFiveBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_DEEP)
                .params("code", CommonUtil.dealReuqestCode(code))
                ;
    }

    public GetRequest<HttpData<PageBean<TradeBean>>> getAllTrade(String code) {
        return OkGo.<HttpData<PageBean<TradeBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_ALL_TRADE)
                .params("stockCode", CommonUtil.dealReuqestCode(code))
                .params("num", 15)
                ;
    }

    public PostRequest<HttpData<PageBean<ZixunListBean>>> getZixunList(String page) {
        return OkGo.<HttpData<PageBean<ZixunListBean>>>post(HttpConfig.BASE_URL + HttpConfig.GET_ZIXUN_LIST)
                .params("pageNumber", page)
                .params("pageSize", 20)
                ;
    }

    public PostRequest<HttpData<PageBean<GuideListBean>>> getGuideList(String page) {
        return OkGo.<HttpData<PageBean<GuideListBean>>>post(HttpConfig.BASE_URL + HttpConfig.GET_GUIDE_LIST)
                .params("type", 2)
                .params("PageNumber", page)
                .params("PageSize", 20)
                ;
    }


    public WebSocketObserver pushCoinFiveDeep(String code) {
        return new WebSocketObserver(HttpConfig.WS_BASE_URL) {
            @Override
            public void onOpen(WebSocket webSocket, okhttp3.Response response, String msg) {
                if (msg == null) {
                    webSocket.send("{\"type\":\"depth_list\",\"list\":[\"" + code + "\"]}");
                } else {
                    webSocket.send(msg);
                }
            }
        };
    }


    public PostRequest<HttpData<HelpBean>> help(String type) {
        return OkGo.<HttpData<HelpBean>>post(HttpConfig.BASE_URL + HttpConfig.HELP)
                .params("type", type)
                ;
    }

    public GetRequest<HttpData<HelpBean>> aboutUs() {
        return OkGo.<HttpData<HelpBean>>get(HttpConfig.BASE_URL + HttpConfig.ABOUT_US)
                ;
    }

    public GetRequest<HttpData<HelpBean>> registerAgreement() {
        return OkGo.<HttpData<HelpBean>>get(HttpConfig.BASE_URL + HttpConfig.REGISTER_AGREEMENT)
                ;
    }

    public GetRequest<HttpData<HelpBean>> privateService() {
        return OkGo.<HttpData<HelpBean>>get(HttpConfig.BASE_URL + HttpConfig.PRIVATE_SERVICE)
                ;
    }

    public PostRequest<HttpData<PageBean<NoticeBean>>> getNotice(String p, String size) {
        return OkGo.<HttpData<PageBean<NoticeBean>>>post(HttpConfig.BASE_URL + HttpConfig.GET_NOTICE)
                .params("pageNumber", p)
                .params("pageSize", size)
                ;
    }

    public GetRequest<HttpData<List<String>>> getHorization() {
        return OkGo.<HttpData<List<String>>>get(HttpConfig.BASE_URL + HttpConfig.CODO_HORIZATION_LIST);
    }
}